package com.atguigu.java4;

import com.atguigu.java2.Employee;
import org.junit.Test;

import java.util.Optional;

/**
 * @author : leimingchao
 * @Description:
 *
 *      Optional类：为了避免在程序中出现空指针异常而创建的
 *
 *      常用方法：
 *          ofNullable(T t)
 *          T orElse(T other)
 *
 * @date Date : 2021年06月19日 20:19
 */
public class OptionalTest {


    /**
     * Optional.of(T t) : 创建一个 Optional 实例，t必须非空；
     * Optional.empty() : 创建一个空的 Optional 实例
     * Optional.ofNullable(T t)：t可以为null
     */
    @Test
    public void test() {
        Girl girl = new Girl();
        girl = null;
        //of(T t):必须保证t是非空的
        Optional<Girl> optionalGirl = Optional.of(girl);
    }

    @Test
    public void test2() {
        Girl girl = new Girl();
        //girl = null;
        //ofNullable(T t)：t可以为空
        Optional<Girl> optionalGirl = Optional.ofNullable(girl);
        System.out.println(optionalGirl);

        //orElse(T t1):如果当前的Optional内部封装的t是非空的，则返回内部的t
        //如果内部的t是空的，则返回orElse()方法中的参数t1
        Girl girl1 = optionalGirl.orElse(new Girl("赵丽颖"));
        System.out.println(girl1);
    }

    @Test
    public void test3() {
        Boy boy = new Boy();
        boy = null;
        String girlName = getGirlName(boy);
        System.out.println(girlName);
    }

    public String getGirlName(Boy boy) {
        return boy.getGirl().getName();
    }

    /**
     * 优化以后的getGirlName()方法
     * @param boy
     * @return
     */
    public String getGirlName1(Boy boy) {

        if (boy != null) {
            Girl girl = boy.getGirl();
            if (girl != null) {
                return girl.getName();
            }
        }
        return null;
    }

    @Test
    public void test4() {
        Boy boy = new Boy();
        boy = null;
        String girlName = getGirlName1(boy);
        System.out.println(girlName);
    }

    /**
     * 使用optional类的getGirlName()
     * @param boy
     * @return
     */
    public String getGirlName2(Boy boy) {
        Optional<Boy> boyOptional = Optional.ofNullable(boy);
        //此时的boy1一定非空
        Boy boy1 = boyOptional.orElse(new Boy(new Girl("迪丽热巴")));
        Girl girl = boy1.getGirl();
        Optional<Girl> girl1 = Optional.ofNullable(girl);
        //此时的girl2一定非空
        Girl girl2 = girl1.orElse(new Girl("古力娜扎"));
        return girl2.getName();
    }

    @Test
    public void test5() {
        Boy boy = null;
        boy = new Boy();
        boy = new Boy(new Girl("佟丽娅"));
        String girlName = getGirlName2(boy);
        System.out.println(girlName);
    }

    /**
     * orElseGet()
     */
    @Test
    public void test6() {
        Optional<Employee> optional = Optional.ofNullable(null);
        Employee employee = optional.orElseGet(Employee::new);
        System.out.println(employee);
    }

    /**
     * map()
     */
    @Test
    public void test7() {
        Optional<Employee> optional = Optional.ofNullable(new Employee(1002,"张三",23,20000.0D));
        Optional<String> str = optional.map(Employee::getName);
        System.out.println(str.get());
    }

    /**
     * flatMap()
     */
    @Test
    public void test8() {
        Optional<Employee> optional = Optional.ofNullable(new Employee(1002,"张三",23,20000.0D));
        Optional<String> str = optional.flatMap(employee -> Optional.of(employee.getName()));
        System.out.println(str.get());
    }

}
